package mobi.greenplate.theweb.repository;

import mobi.greenplate.theweb.domain.RequestRide;
import java.util.List;
import java.util.ArrayList;
import javax.annotation.security.RolesAllowed;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import mobi.greenplate.theweb.domain.User;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("requestRideRepository")
public class RequestRideRepository {

    private EntityManager entityManager;

    @PersistenceContext
    void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public void delete(Long id) {
        final RequestRide forDelete = entityManager.getReference(RequestRide.class, id);
        entityManager.remove(forDelete);
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public RequestRide save(RequestRide requestRide,User requester) {
        requestRide.setRequester(requester);
        return entityManager.merge(requestRide);
    }
    
    @RolesAllowed(value = {"ROLE_USER"})
    public RequestRide findById(Long id) {
        final RequestRide requestRide = entityManager.find(RequestRide.class, id);
        if(null==requestRide){
            throw new IllegalArgumentException("requestRide with id="+id+" not found");
        }
        return requestRide;        
    }

    public int countAll() {
        return ((Integer) entityManager.createQuery("SELECT count(o) from RequestRide o").
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<RequestRide> findAll(int firstResult, int maxResult) {
        final List<RequestRide> founded = entityManager.createQuery("SELECT o FROM RequestRide o").
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<RequestRide>(founded);
    }
    

    //ManyToOne by requester
    public int countByRequester(Long requesterId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from RequestRide o WHERE o.requesterId=:requesterId").
                setParameter("requesterId", requesterId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<RequestRide> findByRequester(int firstResult, int maxResult, Long requesterId) {
        final List<RequestRide> founded = entityManager.createQuery("SELECT o FROM RequestRide o WHERE o.requesterId=:requesterId").
                setParameter("requesterId", requesterId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<RequestRide>(founded);
    }    

    //ManyToOne by startAddress
    public int countByStartAddress(Long startAddressId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from RequestRide o WHERE o.startAddressId=:startAddressId").
                setParameter("startAddressId", startAddressId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<RequestRide> findByStartAddress(int firstResult, int maxResult, Long startAddressId) {
        final List<RequestRide> founded = entityManager.createQuery("SELECT o FROM RequestRide o WHERE o.startAddressId=:startAddressId").
                setParameter("startAddressId", startAddressId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<RequestRide>(founded);
    }    

    //ManyToOne by endAddress
    public int countByEndAddress(Long endAddressId) {
        return ((Integer) entityManager.createQuery("SELECT count(o) from RequestRide o WHERE o.endAddressId=:endAddressId").
                setParameter("endAddressId", endAddressId).
                getSingleResult()).intValue();
    }

    @RolesAllowed(value = {"ROLE_USER"})
    @Transactional
    public List<RequestRide> findByEndAddress(int firstResult, int maxResult, Long endAddressId) {
        final List<RequestRide> founded = entityManager.createQuery("SELECT o FROM RequestRide o WHERE o.endAddressId=:endAddressId").
                setParameter("endAddressId", endAddressId).
                setFirstResult(firstResult).
                setMaxResults(maxResult).getResultList();
        return new ArrayList<RequestRide>(founded);
    }    


    
    
}
